from woniusales_ui_test_ddt.tools.readxls import Readxls
from woniusales_ui_test_ddt.tools.writeresult import WriteResult
import sys
import time

xls_data = Readxls().read_datas()  # 获取xls文件数据

# 开始记录日志，写入头部信息
wr=WriteResult()
header = xls_data[0].keys()
wr.write_header(header)

for data in xls_data[-2:]:
    try:
        module_str = data['包(package)'] + '.' + data['模块(.py)']
        __import__(module_str)
        module = sys.modules[module_str]
        cls = getattr(module, data['类(class)'])
        cls_real = cls()
        method = getattr(cls_real, data['方法(method)'])
        d = data['步骤(args)'].split('\n')
        if d != ['']:
            kwargs = {}
            for i in d:
                k, v = i.split('=')
                kwargs[k] = v
            result=method(**kwargs)
        else:
            result = method()
        if result == data["预期结果(expect)"]:
            data["实际结果(通过/不通过)"] = "通过"
        else:
            data["实际结果(通过/不通过)"] = "不通过"
        data["执行信息"] = "正常执行完成"
    except Exception as e:
        data["实际结果(通过/不通过)"] = "发生了异常"
        data["执行信息"] = str(e)  # 你来 记录一下 具体是哪一行代码出错了？ traceback
    finally:
        data["执行时间"] = time.strftime("%Y-%m-%d %H:%M:%S")
        print(data)
        wr.write_data_row(data)  # 每运行一个用例记录一次日志，防止运行一半停电了。


